package edu.rjh.reflection.reflection_4;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;

public class Test {
    public static void main(String[] args) throws IllegalAccessException, IOException {
        Student student = new Student("张三", 18, "男", "北京", "12345678901");

        Teacher teacher = new Teacher("李四", 18);

        saveObject(student);
    }

    public static void saveObject(Object obj) throws IllegalAccessException, IOException {
        Class<?> clazz = obj.getClass();

        BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));


        Field[] declaredFields = clazz.getDeclaredFields();
        for (Field field : declaredFields) {
            field.setAccessible(true);
            String name = field.getName();
            Object value = field.get(obj);
            bw.write(name + ":" + value);
            bw.newLine();
//            System.out.println(name + ":" + value);
        }
        bw.close();
    }
}
